<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Serialize an array
 *
 * This function first converts any slashes found in the array to a temporary
 * marker, so when it gets unserialized the slashes will be preserved
 *
 * @access    private
 * @param    array
 * @return    string
 */
if (!function_exists('xx_serialize')) {
    function xx_serialize($data)
    {
        if (is_array($data)) {
            foreach ($data as $key => $val) {
                if (is_string($val)) {
                    $data[$key] = str_replace('\\', '{{slash}}', $val);
                }
            }
        } else {
            if (is_string($data)) {
                $data = str_replace('\\', '{{slash}}', $data);
            }
        }

        return serialize($data);
    }
}

// --------------------------------------------------------------------

/**
 * Unserialize
 *
 * This function unserializes a data string, then converts any
 * temporary slash markers back to actual slashes
 *
 * @access    private
 * @param    array
 * @return    string
 */
if (!function_exists('xx_unserialize')) {
    function xx_unserialize($data)
    {
        $data = @unserialize(strip_slashes($data));

        if (is_array($data)) {
            foreach ($data as $key => $val) {
                if (is_string($val)) {
                    $data[$key] = str_replace('{{slash}}', '\\', $val);
                }
            }

            return $data;
        }

        return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data;
    }
}

/**
 * 编码
 */
if (!function_exists('xx_encode')) {
    function xx_encode($data)
    {
        return base64_encode(xx_serialize($data));
    }
}

/**
 * 解码
 */
if (!function_exists('xx_decode')) {
    function xx_decode($data)
    {
        return xx_unserialize(base64_decode($data));
    }
}